﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizzCore;
using BizzCommInterfaces;

namespace BizzUI.Forms
{
    public partial class ProviderSelection : Form
    {
        private string _provider;

        class providerItem
        {
            public string name {get;set;}
            public Object provider { get; set; }
        }

        public ProviderSelection(string provider)
        {
            InitializeComponent();
            _provider = provider;
            listBoxProviders.DisplayMember = "name";
                
            switch (_provider)
            {
                case "inbox":
                    {
                        initListBoxwithInboxes();
                        listBoxProviders.Click += new EventHandler(listBoxInboxProviders_Click);
                        buttonOK.Click += new EventHandler(buttonOKInboxProviders_Click);
                        break;
                    }
                case "outbox":
                    {
                        initListBoxwithOutboxes();
                        listBoxProviders.Click += new EventHandler(listBoxOutboxProviders_Click);
                        buttonOK.Click+=new EventHandler(buttonOKOutboxProviders_Click);
                        break;
                    }
            }
        }


        private void initListBoxwithInboxes()
        {
            BizzSystem s = BizzSystem.getInstance();
            foreach (IInbox inbox in s.getInboxProviders())
            {
                listBoxProviders.Items.Add(new providerItem{ name = inbox.getInboxName(), provider = inbox });
                
            }
            
        }

        private void initListBoxwithOutboxes()
        {
            BizzSystem s = BizzSystem.getInstance();
            foreach (IOutbox outbox in s.getOutboxProviders())
            {
                listBoxProviders.Items.Add(new providerItem { name = outbox.getOutboxName(), provider = outbox });

            }

        }

    

        #region Events

        void buttonOKOutboxProviders_Click(object sender, EventArgs e)
        {
            if (listBoxProviders.SelectedItem != null)
            {
                BizzSystem s = BizzSystem.getInstance();
                IOutbox outbox = ((IOutbox)((providerItem)listBoxProviders.SelectedItem).provider);
                s.setupConcreteOutbox(outbox);
                listBoxProviders.SelectedItem = listBoxProviders.Items[0];
            }
        }


        void buttonOKInboxProviders_Click(object sender, EventArgs e)
        {
            if (listBoxProviders.SelectedItem != null)
            {
                BizzSystem s = BizzSystem.getInstance();
                IInbox inbox = ((IInbox)((providerItem)listBoxProviders.SelectedItem).provider);
                s.setupConcreteInbox(inbox);
                listBoxProviders.SelectedItem = listBoxProviders.Items[0];
            }
        }

        void listBoxOutboxProviders_Click(object sender, EventArgs e)
        {
            if (listBoxProviders.SelectedItem != null)
                textBoxDescription.Lines = ((IOutbox)((providerItem)listBoxProviders.SelectedItem).provider).getDescription();
        }


        void listBoxInboxProviders_Click(object sender, EventArgs e)
        {
            if (listBoxProviders.SelectedItem != null)
                textBoxDescription.Lines = ((IInbox)((providerItem)listBoxProviders.SelectedItem).provider).getDescription();
        }

        private void buttonReady_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }


        #endregion

    }
}
